package jsyf.oa.service.salary;

import com.alibaba.dubbo.common.utils.StringUtils;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import jsyf.com.util.FastJsonConvert;
import jsyf.oa.dao.salary.SalaryMainDao;
import jsyf.oa.entity.doc.TypeEntity;
import jsyf.oa.entity.salary.SalaryMainEntity;
import jsyf.oa.facade.salary.SalaryMainFacade;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * Created by Maqiuqiu on 2018/11/19.
 */
@Service
@Transactional(rollbackFor = Exception.class)
@com.alibaba.dubbo.config.annotation.Service(protocol = {"dubbo"})
public class SalaryMainService implements SalaryMainFacade {
    @Autowired
    private SalaryMainDao salarymainDao;

    @Override
    public String save(String jsonData) {
        SalaryMainEntity entity = JSON.toJavaObject(JSON.parseObject(jsonData), SalaryMainEntity.class);
        Integer rows;
        if ("0".equals(entity.getId()) || StringUtils.isBlank(entity.getId())) {
            rows = salarymainDao.add(entity);
        } else {
            SalaryMainEntity salaryMainEntity = (SalaryMainEntity) salarymainDao.getIBaseEntityById(entity.getId());
            salaryMainEntity.setCompanyId(entity.getCompanyId());
            salaryMainEntity.setCompanyName(entity.getCompanyName());
            salaryMainEntity.setMonth(entity.getMonth());
            salaryMainEntity.setName(entity.getName());
            salaryMainEntity.setUrl(entity.getUrl());
            salaryMainEntity.setCreateUserId(entity.getCreateUserId());
            salaryMainEntity.setCreateTime(entity.getCreateTime());
            rows = salarymainDao.update(salaryMainEntity);
        }
        return rows == 0 ? "" : entity.getId();
    }

    @Override
    public Boolean delById(String jsonData) {
        return salarymainDao.delById(JSON.parseObject(jsonData).getString("id")) > 0;
    }

    @Override
    public JSONObject getList(String jsonData) {
        JSONObject jsonParams = FastJsonConvert.convertJSONToObject(jsonData, JSONObject.class);
        String dateTime = jsonParams.getString("dateTime");
        String companyId = jsonParams.getString("companyId");
        List<JSONObject> records = null;
        /*分页信息*/
        int requestPage = 1;
        int pageSize = Integer.MAX_VALUE;
        StringBuffer condition = new StringBuffer();
        if (jsonParams.containsKey("pageSize")) {
            pageSize = jsonParams.getInteger("pageSize");
        }
        if (jsonParams.containsKey("requestPage")) {
            requestPage = jsonParams.getInteger("requestPage");
        }
        if (dateTime!=null) {
            condition.append(" AND MONTH = "+dateTime+" "+"\n");
        }
        if (companyId!=null) {
            condition.append(" AND COMPANY_ID = "+companyId+" "+"\n");
        }
        records = salarymainDao.getListInfo(condition.toString(),requestPage, pageSize);

        int recordCount = records.size();
        //如果整除表示正好分N页，如果不能整除在N页的基础上+1页
        int pageCount = recordCount % pageSize == 0 ? recordCount / pageSize : (recordCount / pageSize) + 1;
        //存放分页信息
        JSONObject sendData = new JSONObject();
        //存放list
        JSONObject result = new JSONObject();
        sendData.put("recordCount", recordCount);
        sendData.put("pageCount", pageCount);
        sendData.put("requestPage", 1);
        result.put("pager", sendData);
        // 子数组对象
        result.put("records", records);
        return result;
    }
}
